package de.hwr.chronix.client;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import de.hwr.chronix.client.dto.AttendeeDTO;
import de.hwr.chronix.client.dto.PollDTO;
import de.hwr.chronix.client.dto.TimeslotAdminDTO;
import de.hwr.chronix.client.dto.TimeslotAttendeeDTO;
import de.hwr.chronix.shared.validator.FieldVerifier;

public class Formular_Startpage extends Composite {
	private final ChronixServiceAsync chronixService = GWT.create(ChronixService.class);

	private static Formular_StartpageUiBinder uiBinder = GWT
			.create(Formular_StartpageUiBinder.class);
	@UiField
	Button b_abstimmung_anlegen;
	@UiField
	TextBox t_abstimmungs_id;
	@UiField
	Button b_abstimmungs_id_ok;
	@UiField
	TextBox t_admin_id;
	@UiField
	Button b_admin_id_ok;
	

	interface Formular_StartpageUiBinder extends UiBinder<Widget, Formular_Startpage> {
	}

	public Formular_Startpage() {
		initWidget(uiBinder.createAndBindUi(this));
		String id = com.google.gwt.user.client.Window.Location.getParameter("pollid");
		String admin = com.google.gwt.user.client.Window.Location.getParameter("admin");
		if (id != null) {
			t_abstimmungs_id.setText(id);
			loadPollbyID(id);
		} else if (admin != null) {
			t_admin_id.setText(admin);
			loadPollbyAdminID(admin);
		}
	}

	private void loadPollbyAdminID(String admin) {
		b_admin_id_ok.setEnabled(false);
		chronixService.getPollAdmin(admin, new AsyncCallback<PollDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				b_admin_id_ok.setEnabled(true);
			}

			@Override
			public void onSuccess(PollDTO result) {
				gotoVote(result);
			}
		});

	}

	@UiHandler("b_abstimmung_anlegen")
	void onB_abstimmung_anlegenClick(ClickEvent event) {
		Chronix.clearPanel(this);
		Chronix.insertFormular();
	}

	@UiHandler("b_abstimmungs_id_ok")
	void onB_abstimmungs_id_okClick(ClickEvent event) {

		String pollID = t_abstimmungs_id.getText();
		if (FieldVerifier.isValidPollID(pollID)) {
			loadPollbyID(pollID);
		} else {
			Window.alert("AbstimmungsID ist zu kurz");
			// fehler
		}

	}
	@UiHandler("b_admin_id_ok")
	void onB_admin_id_okClick(ClickEvent event) {

		String pollID = t_admin_id.getText();
		if (FieldVerifier.isValidPollID(pollID)) {
			loadPollbyAdminID(pollID);
		} else {
			Window.alert("AdminID ist zu kurz");
			// fehler
		}

	}

	private void loadPollbyID(String pollID) {
		b_abstimmungs_id_ok.setEnabled(false);
		chronixService.getPollUser(pollID, new AsyncCallback<PollDTO>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				b_abstimmungs_id_ok.setEnabled(true);
			}

			@Override
			public void onSuccess(PollDTO result) {
				gotoVote(result);
			}
		});
	}
	

	private void gotoVote(PollDTO result) {
		Chronix.clearPanel(this);
		Chronix.insertCalendarpage(result);
		// Chronix.insertCalendarpage();
	}

	

	
}
